package com.zjh.common.dtgrid.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;

import com.zjh.common.dtgrid.model.Column;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;


/**
 * @author lk
 * @Description: dtgrid工具类，拷贝自大连首闻科技有限公司的DLShouWen Grid
 * @date 2018/6/5 09:25
 */
public class GridUtils {

    /**
     * 格式化内容
     *
     * @param column  dtgrid表格列对象
     * @param content 内容
     * @return 格式化后的内容
     * @throws Exception 异常信息
     */
    public static String formatContent(Column column, String content) throws ParseException {
        //处理码表
        if (column.getCodeTable() != null) {
            if (column.getCodeTable().containsKey(content)) {
                return MapUtils.getString(column.getCodeTable(), content);
            }
        }
        //处理日期、数字的默认情况
        if ("date".equalsIgnoreCase(column.getType()) && column.getFormat() != null && !"".equals(column.getFormat())) {
            if (StringUtils.isNotBlank(column.getOtype())) {
                if ("time_stamp_s".equals(column.getOtype())) {
                    Date date = new Date(Long.parseLong(content) * 1000);
                    return LocalDate.fromDateFields(date).toString(DateTimeFormat.forPattern(column.getFormat()));
                } else if ("time_stamp_ms".equals(column.getOtype())) {
                    Date date = new Date(Long.parseLong(content));
                    return LocalDate.fromDateFields(date).toString(DateTimeFormat.forPattern(column.getFormat()));
                } else if ("string".equals(column.getOtype())) {
                    if (StringUtils.isNotBlank(column.getOformat())) {
                        return LocalDateTime.parse(content, DateTimeFormat.forPattern(column.getOformat().replace("hh:mm:ss", "HH:mm:ss"))).toString(DateTimeFormat.forPattern(column.getFormat().replace("hh:mm:ss", "HH:mm:ss")));
                    }
                }
            }
        } else if ("number".equalsIgnoreCase(column.getType()) && StringUtils.isNotBlank(column.getFormat())) {
            DecimalFormat df = new DecimalFormat(column.getFormat());
            content = df.format(Double.parseDouble(content));
        }

        return content;
    }

}